package beans;

import java.util.ArrayList;
import java.util.List;
import dao.Examination;
import dao.ExaminationDAO;

public class Examinationbean {
    private ExaminationDAO examinationDAO;
    private List<Examination> items1;
    private List<Examination> items2;
    private List<Examination> items3;
    
    public List<Examination> getItems1() {
        return items1;
    }
    public void setItems1(List<Examination> items1) {
        this.items1 = items1;
    }
    public List<Examination> getItems2() {
        return items2;
    }
    public void setItems2(List<Examination> items2) {
        this.items2 = items2;
    }
    public List<Examination> getItems3() {
        return items3;
    }
    public void setItems3(List<Examination> items3) {
        this.items3 = items3;
    }
    public Examinationbean(){
	examinationDAO = new ExaminationDAO();
    }
    public boolean addExamination(Examination examination){
	return examinationDAO.save(examination);
    }
    public void getItems(String chapterid, List<Integer> examinationids, List<Integer> errorList){
	Prelevancebean prelevancebean = new Prelevancebean();
	List<Integer> knowledges = new ArrayList<Integer>();//选出知识点
	if(errorList !=null)
	    if(errorList.size() > 0)
	knowledges.addAll(errorList);//出错的知识点
	
	List<Integer> preleKnowledgeids = new ArrayList<Integer>();
	if(errorList != null)
	    if(errorList.size() > 0)
		preleKnowledgeids = prelevancebean.findKnowledgesByKnowledgeIds(errorList);	
	int knowledgeCounts = knowledges.size();
	if((knowledgeCounts < 15)){
	    if(preleKnowledgeids.size() > 0){
	    List<Integer> preleKnowledgeList = examinationDAO.getPreleKnowledgeids(chapterid, preleKnowledgeids, 15-knowledgeCounts);
	    if(preleKnowledgeList !=null){
		if(preleKnowledgeList.size() > 0)
		knowledges.addAll(preleKnowledgeList);//出错相关的知识点
	    }
	    }
	}
	knowledgeCounts = knowledges.size();
	if(knowledgeCounts < 15){
	    List<Integer> existsKnowledges = new ArrayList<Integer>();
//	    if(knowledgeids !=null)
//	    existsKnowledges.addAll(knowledgeids);	    
	    existsKnowledges.addAll(knowledges);
	    //int otherKnowledgeCounts = examinationDAO.getKnowledgeCount(chapterid, existsKnowledges); 
	    int remainCounts = 15 - knowledgeCounts;		
	    knowledges.addAll(examinationDAO.getKnowledgeids(chapterid, existsKnowledges, remainCounts));
	    //其他的知识点	    
	}
	knowledgeCounts = knowledges.size();
	if(knowledgeCounts<=5){
	    items1 = examinationDAO.getExaminationByType2(chapterid, 1, examinationids, 5, knowledges);
	    items2 = examinationDAO.getExaminationByType2(chapterid, 2, examinationids, 5, knowledges);
	    items3 = examinationDAO.getExaminationByType2(chapterid, 3, examinationids, 5, knowledges);
	}else if(knowledgeCounts <=10){
	    items1 = examinationDAO.getExaminationByType2(chapterid, 1, examinationids, 5, knowledges.subList(0, 5));
	    items2 = examinationDAO.getExaminationByType2(chapterid, 2, examinationids, 5, knowledges.subList(5, knowledgeCounts));
	    items3 = examinationDAO.getExaminationByType2(chapterid, 3, examinationids, 5, knowledges.subList(0, 4));
	}else if(knowledgeCounts <=15){
	    items1 = examinationDAO.getExaminationByType2(chapterid, 1, examinationids, 5, knowledges.subList(0, 5));
	    items2 = examinationDAO.getExaminationByType2(chapterid, 2, examinationids, 5, knowledges.subList(5, 10));
	    items3 = examinationDAO.getExaminationByType2(chapterid, 3, examinationids, 5, knowledges.subList(10, knowledgeCounts));
	}else{
	    items1 = examinationDAO.getExaminationByType2(chapterid, 1, examinationids, 5, knowledges.subList(0, 5));
	    items2 = examinationDAO.getExaminationByType2(chapterid, 2, examinationids, 5, knowledges.subList(5, 10));
	    items3 = examinationDAO.getExaminationByType2(chapterid, 3, examinationids, 5, knowledges.subList(10, 15));
	}
		
    }
    
    public void getItems2(List<Integer> examinationids, List<Integer> errorList){
	Prelevancebean prelevancebean = new Prelevancebean();
	List<Integer> knowledges = new ArrayList<Integer>();//选出知识点
	if(errorList !=null)
	    if(errorList.size() > 0)
	knowledges.addAll(errorList);//出错的知识点
	
	List<Integer> preleKnowledgeids = new ArrayList<Integer>();
	if(errorList != null)
	    if(errorList.size() > 0)
		preleKnowledgeids = prelevancebean.findKnowledgesByKnowledgeIds(errorList);	
	int knowledgeCounts = knowledges.size();
	if((knowledgeCounts < 25)){
	    if(preleKnowledgeids.size() > 0){
	    List<Integer> preleKnowledgeList = examinationDAO.getPreleKnowledgeids2(preleKnowledgeids, 25-knowledgeCounts);
	    if(preleKnowledgeList !=null){
		if(preleKnowledgeList.size() > 0)
		knowledges.addAll(preleKnowledgeList);//出错相关的知识点
	    }
	    }
	}
	knowledgeCounts = knowledges.size();
	if(knowledgeCounts < 25){
	    List<Integer> existsKnowledges = new ArrayList<Integer>();
//	    if(knowledgeids !=null)
//	    existsKnowledges.addAll(knowledgeids);	    
	    existsKnowledges.addAll(knowledges);
	    int remainCounts = 25 - knowledgeCounts;
	    knowledges.addAll(examinationDAO.getKnowledgeids2(existsKnowledges, remainCounts));
	    
	    //其他的知识点	    
	}
	knowledgeCounts = knowledges.size();
	if(knowledgeCounts<=5){
	    items1 = examinationDAO.getExaminationByType3(1, examinationids, 5, knowledges);
	    items2 = examinationDAO.getExaminationByType3(2, examinationids, 5, knowledges);
	    items3 = examinationDAO.getExaminationByType3(3, examinationids, 5, knowledges);
	}else if(knowledgeCounts <=10){
	    items1 = examinationDAO.getExaminationByType3(1, examinationids, 5, knowledges.subList(0, 5));
	    items2 = examinationDAO.getExaminationByType3(2, examinationids, 5, knowledges.subList(5, knowledgeCounts));
	    items3 = examinationDAO.getExaminationByType3(3, examinationids, 5, knowledges.subList(0, 4));
	}else if(knowledgeCounts <=15){
	    items1 = examinationDAO.getExaminationByType3(1, examinationids, 5, knowledges.subList(0, 5));
	    items2 = examinationDAO.getExaminationByType3(2, examinationids, 5, knowledges.subList(5, 10));
	    items3 = examinationDAO.getExaminationByType3(3, examinationids, 5, knowledges.subList(10, knowledgeCounts));
	}else if(knowledgeCounts <=20){
	    items1 = examinationDAO.getExaminationByType3(1, examinationids, 10, knowledges.subList(0, 10));
	    items2 = examinationDAO.getExaminationByType3(2, examinationids, 10, knowledges.subList(10, 15));
	    items3 = examinationDAO.getExaminationByType3(3, examinationids, 5, knowledges.subList(15, knowledgeCounts));
	}
	else if(knowledgeCounts <=25){
	    items1 = examinationDAO.getExaminationByType3(1, examinationids, 10, knowledges.subList(0, 10));
	    items2 = examinationDAO.getExaminationByType3(2, examinationids, 10, knowledges.subList(10, 20));
	    items3 = examinationDAO.getExaminationByType3(3, examinationids, 5, knowledges.subList(20, knowledgeCounts));
	}else{
	    items1 = examinationDAO.getExaminationByType3(1, examinationids, 10, knowledges.subList(0, 10));
	    items2 = examinationDAO.getExaminationByType3(2, examinationids, 10, knowledges.subList(10, 20));
	    items3 = examinationDAO.getExaminationByType3(3, examinationids, 5, knowledges.subList(20, 25));
	}
		
    }
    
    public static void main(String[] args){
	Examinationbean examinationbean = new Examinationbean();
	List<Integer> list1 = new ArrayList<Integer>();
	List<Integer> list2 = new ArrayList<Integer>();
	list1.add(3);
	list1.add(4);
	list1.add(5);
	list2.add(4);
	examinationbean.getItems("010101", list1, list2);
	System.out.println(examinationbean.getItems3().size());
    }
}
